class Person(object):
    def __init__(self,name,age):
        self.name = name
        self.age = age
    def __str__(self):
        return "name:%s,age:%d"%(self.name, self.age)
    def run(self):
        print("%s在跑",self.name)
    def __getattr__(self, item):
        if item in ["height", "weight"]:
            return 180
        else:
            raise AttributeError

    def __setattr__(self, key, value):

        if key == "age" and value < 0:
            print("-------1")
            # self.age = 0 不可以这样写
            self.__dict__[key] = 0
        else:
            print("-------2")
            self.__dict__[key] = value

    # 以后避免实现该方法
    # def __getattribute__(self, item):
    #     print("--------------cao", item)
    #     super().__getattribute__(item)


'''
__getattr__:拦截点号运算

'''
per = Person("tom", 20)

# print(per.weight)
# print(per.height)

'''
__setattr__:会拦截所有属性的赋值语句
注意：如果重写此方法，在内部不能写self.attr = value,会调用__setattr__()
如此形成死循环引用
'''
per.age = -30
print(per.age)